package com.example.neusoft215gdh.androidpro_21.Model;

import com.example.neusoft215gdh.androidpro_21.Interface.ActivityFace;
import com.example.neusoft215gdh.androidpro_21.JavaBean.Activitys;
import com.example.neusoft215gdh.androidpro_21.JavaBean.MySelfBean;
import com.example.neusoft215gdh.androidpro_21.Listener.ActivityDetailListener;
import com.example.neusoft215gdh.androidpro_21.Listener.ActivityListener;
import com.example.neusoft215gdh.androidpro_21.Listener.AlterUserInfo;
import com.example.neusoft215gdh.androidpro_21.Listener.MyListener;
import com.example.neusoft215gdh.androidpro_21.Listener.OrderListener;
import com.example.neusoft215gdh.androidpro_21.Service.ActivitysService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
/**
 * Created by hp-pc on 2017/10/12.
 */

public class ActivityModel implements ActivityFace{
    private Retrofit retrofit;
   ActivitysService activitysService;
    private static final String BASE_URL = "http://172.24.10.175/workout/api.php/";//主机地址

    public ActivityModel(){
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    @Override
    public void userInfo(String userid,final MyListener listener) {
        activitysService=retrofit.create(ActivitysService.class);
        Call<MySelfBean> call=activitysService.getMySelf(userid);
        call.enqueue(new Callback<MySelfBean>() {
            @Override
            public void onResponse(Call<MySelfBean> call, Response<MySelfBean> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listener.onResponse(response.body());
                } else {
                    listener.onFail("onresponse fail");
                }
            }

            @Override
            public void onFailure(Call<MySelfBean> call, Throwable t) {

            }
        });
    }

    @Override
    public void getActivitys(String sessionId, int page,final ActivityListener listener) {
        activitysService=retrofit.create(ActivitysService.class);
        Call<List<Activitys>> call=activitysService.getActivitys(sessionId,page);
        call.enqueue(new Callback<List<Activitys>>() {
            @Override
            public void onResponse(Call<List<Activitys>> call, Response<List<Activitys>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listener.onResponse(response.body());
                } else {
                    listener.onFail("onresponse fail");
                }
            }

            @Override
            public void onFailure(Call<List<Activitys>> call, Throwable t) {

            }
        });
    }

    /**
     * 获得活动详情
     */


    @Override
    public void getActivityById( String id,final ActivityDetailListener listener) {
        activitysService = retrofit.create(ActivitysService.class);
        Call<Activitys> call= activitysService.getActivityById(id);
        call.enqueue(new Callback<Activitys>() {
            @Override
            public void onResponse(Call<Activitys> call, Response<Activitys> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listener.onResponse(response.body());
                } else {
                    listener.onFail("onresponse fail");
                }
            }

            @Override
            public void onFailure(Call<Activitys> call, Throwable t) {

            }
        });
    }


    //活动订单
    @Override
    public void signActivity( String acname,String activity_id,String telephone,String Anum,String Stunum,String Pay_type,
                              String Aprice,String notice,int userid,final OrderListener listener) {
        activitysService = retrofit.create(ActivitysService.class);
        Call<Integer> call= activitysService.signActivity("signactivity",acname,activity_id,telephone,Anum,Stunum,Pay_type,Aprice,notice,"保险详细内容",userid,"lgj98rfkiqvenhrk2o7oe2po81");
        call.enqueue(new Callback<Integer>() {
            @Override
            public void onResponse(Call<Integer> call, Response<Integer> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listener.onResponse(response.body());
                } else {
                    listener.onFail("onresponse fail");
                }
            }

            @Override
            public void onFailure(Call<Integer> call, Throwable t) {

            }
        });
    }

    @Override
    public void alterUserInfo( String session, String userid, String name,final AlterUserInfo listener) {
        activitysService = retrofit.create(ActivitysService.class);
        Call<String> call= activitysService.editTel(session,userid,name);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listener.onResponse(response.body());
                } else {
                    listener.onFail("onresponse fail");
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {

            }
        });
    }

    @Override
    public void clubAttention(String SessionID, String userid, String fan_id, int fans_type, final AlterUserInfo listener) {
        activitysService = retrofit.create(ActivitysService.class);
        Call<String> call= activitysService.love(SessionID,userid,fan_id,fans_type);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listener.onResponse(response.body());
                } else {
                    listener.onFail("onresponse fail");
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {

            }
        });
    }

}
